package com.lhg.service;

import com.lhg.entity.User;
import com.lhg.mapper.UserMapper;
import com.lhg.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }

    public void insertUser(String username, String password) {
        userMapper.insertUser(username, password);
    }

    public User findByAuthorization(String authHeader){
        // 解析Authorization请求头中的JWT令牌 Bearer access_token
        String token = authHeader.substring(7);
        String username = jwtTokenUtil.getUsernameFromToken(token);
        User foundUser = userMapper.findByUsername(username);
        return foundUser;
    }
}
